package com.movie.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.movie.beans.MovieBeans;

public class MovieDao {
	
	private Connection road;
	private ResultSet result;
	private PreparedStatement car;
	private DataSource ds;
		
	public MovieDao(){
		try{		
			Context init = new InitialContext();
			ds = (DataSource)init.lookup("java:comp/env/SJHDB");
		}catch(Exception e){
			e.printStackTrace();
			return;		
		}
	}
	public void viewappointedMovie(){
		
	}
	public void searchMovie(){
		
	}
	public ArrayList<MovieBeans> viewrecentMovie(){
		ArrayList<MovieBeans> list= new ArrayList<MovieBeans>();
		
		boolean present = false;
		String sql = "SELECT MCODE, MVNAME, GCODE, COUNTRY, RTIME,ODATE,"
				+ "DIRECTOR, ACTOR, FRATE, STORY, IMAGE FROM MOVIES";
		try{
			road = ds.getConnection();
			
			car = road.prepareStatement(sql);
						
			result = car.executeQuery();
			while(result.next()){
				if(result.getNString(1) != null){ present = true; 
					MovieBeans mb=new MovieBeans();	
					mb.setMcode(result.getNString(1));
					mb.setMvname(result.getNString(2));
					mb.setGcode(result.getNString(3));
					mb.setCountry(result.getNString(4));
					mb.setRtime(result.getNString(5));
					mb.setOdate(result.getNString(6));
					mb.setDirector(result.getNString(7));
					mb.setActor(result.getNString(8));
					mb.setFrate(result.getNString(9));
					mb.setStory(result.getNString(10));
					mb.setImage(result.getNString(11));
					list.add(mb);
				}
				
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(result != null){ try{ result.close();}catch(SQLException e){}	}
			if(car != null){ try{ car.close();}catch(SQLException e){}	}
			if(road != null){ try{ road.close();}catch(SQLException e){}	}
		}
		return list;
	}
	public void viewhighMovie(){
		
	}
}
